package com.ghck.shop.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ghck.server.exception.ErrorConstant;
import com.ghck.shop.dao.BannerCategoryMapper;
import com.ghck.shop.dao.GoodCollectMapper;
import com.ghck.shop.dao.GoodHotSearchMapper;
import com.ghck.shop.dao.GoodsMapper;
import com.ghck.shop.dao.GoodsSpecificationMapper;
import com.ghck.shop.dao.HotCategoryMapper;
import com.ghck.shop.dao.UserAreaMapper;
import com.ghck.shop.exception.ShopServerException;
import com.ghck.shop.model.BannerCategory;
import com.ghck.shop.model.Category;
import com.ghck.shop.model.CommentLabel;
import com.ghck.shop.model.GoodCollect;
import com.ghck.shop.model.GoodHotSearch;
import com.ghck.shop.model.GoodsImage;
import com.ghck.shop.model.GoodsSortConfig;
import com.ghck.shop.model.GoodsSpecification;
import com.ghck.shop.model.HotCategory;
import com.ghck.shop.model.MarketingActivityGoods;
import com.ghck.shop.model.PostageConfig;
import com.ghck.shop.model.SystemCity;
import com.ghck.shop.model.UserArea;
import com.ghck.shop.model.request.GoodAddReq;
import com.ghck.shop.model.request.GoodsCommentReq;
import com.ghck.shop.model.request.GoodsReq;
import com.ghck.shop.model.response.CategoryRes;
import com.ghck.shop.model.response.GoodCollectRes;
import com.ghck.shop.model.response.GoodsCommentRes;
import com.ghck.shop.model.response.GoodsRes;
import com.ghck.shop.model.response.MarketingActivityRes;
import com.ghck.shop.service.GoodsService;
import com.ghck.shop.utils.FileStoreUtil;


/** 
 * 
 * @author chenxuan
 *
 */
@Service
@Transactional
public class GoodsServiceImpl implements GoodsService{
	
	@Autowired
	private GoodsMapper goodsMapper;
	@Autowired
	private HotCategoryMapper hotCategoryMapper;
	@Autowired
	private GoodsSpecificationMapper goodsSpecificationMapper;
	@Autowired
	private GoodCollectMapper goodCollectMapper;
	@Autowired
	private BannerCategoryMapper bannerCategoryMapper;
	@Autowired
	private UserAreaMapper userAreaMapper;
	@Autowired
	private GoodHotSearchMapper goodHotSearchMapper;
	
	private static Logger logger = LoggerFactory.getLogger(GoodsServiceImpl.class);
	
	@Override
	public void subtractStock(Integer goodsId, Integer num) {
		GoodsRes gr = goodsMapper.getGoodsDetailById(goodsId);
		if(null == gr){
			throw new ShopServerException(ErrorConstant.OPERATION_ERROR.getErrorCode(), "未找到该商品");
		}
		if(gr.getStockStatus() == 1){
			throw new ShopServerException(ErrorConstant.OPERATION_ERROR.getErrorCode(), "该商品库存已经被锁定");
		}
		//是否需要减库存
		if(gr.getSubtract() == 1){
			if(gr.getAmount() > 0){
				goodsMapper.subtractStock(goodsId, num);
			}else{
				throw new ShopServerException(ErrorConstant.OPERATION_ERROR.getErrorCode(), "该商品已经没有库存");
			}
		}
		
	}

	@Override
	public void increaseStock(Integer goodsId, Integer num) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public GoodsCommentReq addGoodsComment(GoodsCommentReq goodsCommentReq) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<CategoryRes> getHomeList(GoodsReq goodsReq) {
		
		List<CategoryRes> list = goodsMapper.homeCategoryList();
		for(CategoryRes cr : list){
			GoodsReq gr = new GoodsReq();
			gr.setStart(0);
			gr.setLimit(6);
			gr.setCategoryId(cr.getId());
			if(null != cr.getImage() && !cr.getImage().trim().isEmpty()){
				cr.setImage(FileStoreUtil.getBaseUrl().concat(cr.getImage()));
			}
			List<GoodsRes> goodsList = goodsMapper.findGoodsByCategoryId(gr);
			for(GoodsRes gres : goodsList){
				if(null != gres.getThumbnailImage() && !gres.getThumbnailImage().trim().isEmpty()){
					gres.setThumbnailImage(FileStoreUtil.getBaseUrl().concat(gres.getThumbnailImage()));
				}
			}
			cr.setGoodsList(goodsList);
		}
		return list;
	}
	
	@Override
	public List<CategoryRes> getBannerList(GoodsReq goodsReq) {
		List<CategoryRes> result = new ArrayList<CategoryRes>();
		List<BannerCategory> bcs = bannerCategoryMapper.allBannerCategorys();
		if(CollectionUtils.isNotEmpty(bcs)){
			List<Integer> ids = new ArrayList<Integer>();
			for(BannerCategory bc:bcs){
				ids.add(bc.getCategoryId());
			}
			result.addAll(goodsMapper.getCategoryByIds(ids));
		}
		for(CategoryRes cr:result){
			if(StringUtils.isNotEmpty(cr.getImage())){
				cr.setImage(FileStoreUtil.getBaseUrl().concat(cr.getImage()));
			}
		}
		return result;
	}

	@Override
	public GoodsRes goodsDetail(GoodsReq goodReq) {
		if(goodReq.getId() == null || goodReq.getId() <=0){
			throw new ShopServerException(ErrorConstant.PARAM_ERROR.getErrorCode(),"商品ID不能为空");
		}
		GoodsRes goodsRes = goodsMapper.getGoodsDetailById(goodReq.getId());
		if(goodsRes!=null){
			if(StringUtils.isNotEmpty(goodsRes.getThumbnailImage())){
				goodsRes.setThumbnailImage(FileStoreUtil.getBaseUrl().concat(goodsRes.getThumbnailImage()));
			}
			List<GoodsImage> goodsImageList = goodsMapper.getGoodsImageList(goodReq.getId());
			for(GoodsImage gi : goodsImageList){
				if(StringUtils.isNotEmpty(gi.getImageLarge())){
					gi.setImageLarge(FileStoreUtil.getBaseUrl().concat(gi.getImageLarge()));
				}
				gi.setImageSmall(null);
				if(StringUtils.isNotEmpty(gi.getImageSmall())){
					gi.setImageSmall(FileStoreUtil.getBaseUrl().concat(gi.getImageSmall()));
				}
			}
			goodsRes.setGoodsImageList(goodsImageList);
			/**拉取商品规格*/
			List<GoodsSpecification> goodsSpecifications = goodsSpecificationMapper.findGoodsSpecificationsByGoodId(goodsRes.getId());
			goodsRes.setSpecifications(goodsSpecifications);
			if(goodReq.getUserId() !=null && goodReq.getUserId()>0){
				GoodCollect gc = goodCollectMapper.findGoodCollectByUserIdAndGoodId(goodReq.getUserId(), goodsRes.getId());
				if(gc != null){
					goodsRes.setIsCollect(gc.getId());
				}
			}
			List<Category> categories = goodsMapper.findGoodCategoryByGoodId(goodsRes.getId());
			goodsRes.setCategoryList(categories);
			
		}
		return goodsRes;
	}
	@Override
	public GoodsRes goodsDetailById(Integer goodsId) {
		if(goodsId == null || goodsId <=0){
			throw new ShopServerException(ErrorConstant.PARAM_ERROR.getErrorCode(),"商品ID不能为空");
		}
		GoodsRes goodsRes = goodsMapper.getGoodsDetailById(goodsId);
		if(goodsRes!=null){
			if(StringUtils.isNotEmpty(goodsRes.getThumbnailImage())){
				goodsRes.setThumbnailImage(FileStoreUtil.getBaseUrl().concat(goodsRes.getThumbnailImage()));
			}
			List<GoodsImage> goodsImageList = goodsMapper.getGoodsImageList(goodsId);
			for(GoodsImage gi : goodsImageList){
				if(StringUtils.isNotEmpty(gi.getImageLarge())){
					gi.setImageLarge(FileStoreUtil.getBaseUrl().concat(gi.getImageLarge()));
				}
				gi.setImageSmall(null);
				if(StringUtils.isNotEmpty(gi.getImageSmall())){
					gi.setImageSmall(FileStoreUtil.getBaseUrl().concat(gi.getImageSmall()));
				}
			}
			goodsRes.setGoodsImageList(goodsImageList);
			/**拉取商品规格*/
			List<GoodsSpecification> goodsSpecifications = goodsSpecificationMapper.findGoodsSpecificationsByGoodId(goodsRes.getId());
			goodsRes.setSpecifications(goodsSpecifications);
		}
		
		return goodsRes;
	}

	@Override
	public List<SystemCity> getAllRegion() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Category> categoryList() {
		//List<Category> list = goodsMapper.categoryList();
		return this.categoryListLevel();
	}
	

	@Override
	public List<Category> categoryListLevel() {
		List<Category> result = new ArrayList<Category>();
		Category first = new Category();
		first.setTitle("热卖推荐");
		first.setId(9999);
		first.setLevel(1);
		first.setParentId(0);
		result.add(first);
		List<HotCategory> hotCategories = hotCategoryMapper.listAllHotCategorys();
		Set<Integer> hotMap = new HashSet<Integer>();
		if(CollectionUtils.isNotEmpty(hotCategories)){
			List<Integer> ids = new ArrayList<Integer>();
			for(HotCategory hc:hotCategories){
				ids.add(hc.getCategoryId());
				hotMap.add(hc.getCategoryId());
			}
			List<Category>  list = hotCategoryMapper.findCategoryByIds(ids);
			if(CollectionUtils.isNotEmpty(list)){
				for(Category c:list){
					c.setAddHot(1);
					c.setRecommend(c.getRecommend());
				}
				first.setChildCategorys(list);
			}
		}
		List<Category> list = goodsMapper.allCategoryList();
		if(CollectionUtils.isNotEmpty(list)){
			Map<Integer, List<Category>> map = new HashMap<Integer, List<Category>>();
			Map<Integer, Category> father = new HashMap<Integer,Category>();
			List<BannerCategory>  bannerCategoryList = bannerCategoryMapper.allBannerCategorys();
			for(Category item:list){
				Integer id = item.getId();
				if(item.getLevel()!=1 && hotMap.contains(id)){
					item.setAddHot(1);
				}
				
				if(item.getLevel() == 1){
					map.put(id, new ArrayList<Category>());
					father.put(id, item);
					
				}else if(item.getLevel() == 2){
					List<Category> temp = map.get(item.getParentId());
					if(temp == null){
						temp = new ArrayList<Category>();
						map.put(item.getParentId(), temp);
					}
					temp.add(item);
				}
				for(BannerCategory bc : bannerCategoryList){
					if(bc.getCategoryId().intValue() == id.intValue()){
						item.setIsRecommendBanner(1);
					}
				}
			}
			for(Entry<Integer,Category> entry: father.entrySet()){
				Category c = entry.getValue();
				result.add(c);
				if(map.containsKey(entry.getKey())){
					c.setChildCategorys(map.get(entry.getKey()));
				}
			}
		}
		
		return result;
	}

	@Override
	public List<GoodsSortConfig> sortList() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<GoodsRes> goodsList(GoodsReq goodsReq) {
		List<GoodsRes> list = goodsMapper.goodsList(goodsReq);
		for(GoodsRes gr : list){
			if(StringUtils.isNotBlank(gr.getThumbnailImage())){
				gr.setThumbnailImage(FileStoreUtil.getBaseUrl().concat(gr.getThumbnailImage()));
			}
		}
		return list;
	}
	
	@Override
	public List<GoodCollectRes> myGoodCollects(GoodsReq goodsReq) {
		List<GoodCollectRes> list = goodsMapper.goodsCollectList(goodsReq);
		for(GoodsRes gr : list){
			if(StringUtils.isNotBlank(gr.getThumbnailImage())){
				gr.setThumbnailImage(FileStoreUtil.getBaseUrl().concat(gr.getThumbnailImage()));
			}
		}
		return list;
	}

	@Override
	public MarketingActivityRes marketingActivityGoodsList(GoodsReq goodsReq) {
		return null;
	}

	@Override
	public List<CommentLabel> commentLabelList() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<GoodsCommentRes> goodsCommentList(GoodsCommentReq goodsCommentReq) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public PostageConfig getPostageConfigByProvinceId(Integer provinceId) {
		
		return goodsMapper.getPostageConfigByProvinceId(provinceId);
	}

	@Override
	public List<GoodsRes> managerList(GoodsReq goodsReq) {
		List<GoodsRes> list = goodsMapper.managerList(goodsReq);
		return list;
	}
	
	@Override
	public Integer countManagerList(GoodsReq goodsReq) {
		return goodsMapper.countManagerList(goodsReq);
	}


	@Override
	public MarketingActivityRes marketingActivityGoodsList(Integer type,Integer start, Integer limit) {
		MarketingActivityRes marketingActivityRes = goodsMapper.queryMarketingActivityByType(type);
		if(marketingActivityRes != null ){
			GoodsReq goodsReq = new GoodsReq();
			goodsReq.setMarketingActivityId(marketingActivityRes.getId());
			goodsReq.setType(type);
			goodsReq.setStart(start);
			goodsReq.setLimit(limit);
			List<GoodsRes> goodsList = goodsMapper.marketingActivityGoodsList(goodsReq);
			if(CollectionUtils.isNotEmpty(goodsList)){
				for(GoodsRes gr : goodsList){
					if(StringUtils.isNoneEmpty(gr.getThumbnailImage())){
						gr.setThumbnailImage(FileStoreUtil.getBaseUrl().concat(gr.getThumbnailImage()));
					}
				}
				marketingActivityRes.setGoodsList(goodsList);
			}
		}
		return marketingActivityRes;
	}
	
	@Override
	@Transactional
	public void updateGood(GoodAddReq goodAddReq) {
		Integer goodId = goodAddReq.getId();
		goodsMapper.updateGoods(goodAddReq);
		
		//处理商品图片
		List<GoodsImage> goodsImages = goodAddReq.getGoodsImageList();
		if(CollectionUtils.isNotEmpty(goodsImages)){
			List<Integer> imageIds = new ArrayList<Integer>();
			List<GoodsImage> needAddImages = new ArrayList<GoodsImage>();
			for(GoodsImage goodsImage:goodsImages){
				goodsImage.setGoodsId(goodId);
				if(goodsImage.getId() !=null && goodsImage.getId() > 0){
					imageIds.add(goodsImage.getId());
				}else {
					needAddImages.add(goodsImage);
				}
			}
			if(CollectionUtils.isNotEmpty(imageIds)){
				goodsMapper.deleteGoodsImageByGoodIdAndNotIds(goodId, imageIds);
			}else {
				goodsMapper.deleteGoodsImageByGoodId(goodId);
			}
			if(CollectionUtils.isNotEmpty(needAddImages)){
				goodsMapper.batchSaveGoodsImages(needAddImages);
			}
		}else {
			goodsMapper.deleteGoodsImageByGoodId(goodId);
		}
		//处理商品分类
		List<Category> categoryList = goodAddReq.getCategoryList();
		if(CollectionUtils.isNotEmpty(categoryList)){
			List<Integer> categoryIds = new ArrayList<Integer>();
			for(Category category:categoryList){
				categoryIds.add(category.getId());
			}
			goodsMapper.deleteGoodCategorysByGoodId(goodId);
			goodsMapper.batchSaveGoodsCategory(goodId, categoryIds);
		}
		//处理规格
		List<GoodsSpecification> specifications = goodAddReq.getSpecifications();
		if(CollectionUtils.isNotEmpty(specifications)){
			List<Integer> needNotDeteleIds = new ArrayList<Integer>();
			List<GoodsSpecification> needUpdates = new ArrayList<GoodsSpecification>();
			List<GoodsSpecification> needAdds = new ArrayList<GoodsSpecification>();
			for(GoodsSpecification goodsSpecification:specifications){
				if(goodsSpecification.getId() != null && goodsSpecification.getId() > 0){
					needNotDeteleIds.add(goodsSpecification.getId());
					needUpdates.add(goodsSpecification);
				}else {
					Date date = new Date();
					goodsSpecification.setGoodId(goodId);
					goodsSpecification.setStatus(0);
					goodsSpecification.setCreateTime(date);
					goodsSpecification.setUpdateTime(date);
					goodsSpecification.setPercentageRate(0);
					needAdds.add(goodsSpecification);
				}
			}
			if(CollectionUtils.isNotEmpty(needNotDeteleIds)){
				goodsSpecificationMapper.deleteGoodsSpecificationByGoodIdAndNotIds(goodId, needNotDeteleIds);
			}
			if(CollectionUtils.isNotEmpty(needAdds)){
				goodsSpecificationMapper.batchSaveGoodsSpecifications(needAdds);
			}
			if(CollectionUtils.isNotEmpty(needUpdates)){
				for(GoodsSpecification goodsSpecification:needUpdates){
					goodsSpecificationMapper.update(goodsSpecification);
				}
			}
		}
	}

	@Override
	@Transactional
	public GoodAddReq addGood(GoodAddReq goodAddReq) {
		if(goodAddReq.getMarketPrice() == null){
			goodAddReq.setMarketPrice(0);
		}
		if(goodAddReq.getPercentageRate() == null){
			goodAddReq.setPercentageRate(0);
		}
		goodsMapper.saveGoods(goodAddReq);
		Integer goodId = goodAddReq.getId();
		if(goodId!=null && goodId > 0){
			/**处理商品图片*/
			List<GoodsImage> goodsImageList =  goodAddReq.getGoodsImageList();
			if(CollectionUtils.isNotEmpty(goodsImageList)){
				for(GoodsImage gi:goodsImageList){
					gi.setGoodsId(goodId);
				}
				/**保存图片*/
				goodsMapper.batchSaveGoodsImages(goodsImageList);
			}
			/**处理商品分类*/
			List<Category> categoryList = goodAddReq.getCategoryList();
			if(CollectionUtils.isNotEmpty(categoryList)){
				List<Integer> categoryIds = new ArrayList<Integer>(categoryList.size());
				for(Category c:categoryList){
					categoryIds.add(c.getId());
				}
				goodsMapper.batchSaveGoodsCategory(goodId, categoryIds);
			}
			/**处理商品规格*/
			List<GoodsSpecification> specifications = goodAddReq.getSpecifications();
			if(CollectionUtils.isNotEmpty(specifications)){
				Date date = new Date();
				for(GoodsSpecification gs:specifications){
					gs.setGoodId(goodId);
					gs.setCreateTime(date);
					gs.setUpdateTime(date);
					gs.setStatus(0);
					if(gs.getPercentageRate() == null){
						gs.setPercentageRate(0);
					}
					if(gs.getCostPrice() == null){
						gs.setCostPrice(0);
					}
					if(gs.getMarketPrice() == null){
						gs.setMarketPrice(0);
					}
				}
				goodsSpecificationMapper.batchSaveGoodsSpecifications(specifications);
			}
			
		} else {
			throw new ShopServerException(ErrorConstant.OPERATION_ERROR.getErrorCode(), "添加商品失败"); 
		}
		return goodAddReq;
	}

	@Override
	@Transactional
	public Integer deleteGoodsSpecification(Integer id) {
		GoodsSpecification gs = goodsSpecificationMapper.findGoodsSpecificationById(id);
		if(gs!=null){
			goodsSpecificationMapper.deleteGoodsSpecificationById(id);
		}
		return -1;
	}

	@Override
	public Integer deleteGoodImage(Integer imageId) {
		GoodsImage gi = goodsMapper.findGoodsImageById(imageId);
		if(gi!=null){
			goodsMapper.deleteGoodsImageById(imageId);
		}
		return -1;
	}

	@Override
	public List<GoodsRes> findGoodsBySupplierId(Integer supplierId,Integer isOnsale,Integer start,Integer limit) {
		List<GoodsRes> list = goodsMapper.findGoodsBySupplierId(supplierId, isOnsale, start, limit);
		for(GoodsRes gr : list){
			if(StringUtils.isNotEmpty(gr.getThumbnailImage())){
				gr.setThumbnailImage(FileStoreUtil.getBaseUrl().concat(gr.getThumbnailImage()));
			}
			List<GoodsImage> goodsImages = goodsMapper.getGoodsImageList(gr.getId());
			for(GoodsImage gImage : goodsImages){
				gImage.setImageLarge(FileStoreUtil.getBaseUrl().concat(gImage.getImageLarge()));
				gImage.setImageSmall(FileStoreUtil.getBaseUrl().concat(gImage.getImageSmall()));
			}
			gr.setGoodsImageList(goodsImages);
			/**拉取商品规格*/
			List<GoodsSpecification> goodsSpecifications = goodsSpecificationMapper.findGoodsSpecificationsByGoodId(gr.getId());
			gr.setSpecifications(goodsSpecifications);
			List<Category> categoryList = goodsMapper.findGoodCategoryByGoodId(gr.getId());
			gr.setCategoryList(categoryList);
		}
		return list;
	}

	@Override
	@Transactional
	public void deleteGood(Integer supplierId, Integer goodId) {
		GoodsRes goodsRes = goodsMapper.getGoodsByIdAndSupplierId(goodId, supplierId);
		if(goodsRes!=null){
			goodsMapper.deleteGoodById(goodId);
		}
	}
	
	@Override
	@Transactional
	public void deleteGoodAdmin( Integer goodId) {
		GoodsRes goodsRes = goodsMapper.getGoodsDetailById(goodId);
		if(goodsRes!=null){
			goodsMapper.deleteGoodById(goodId);
		}
	}
	
	@Override
	@Transactional
	public void revieweGood(Integer supplierId, Integer goodId) {
		GoodsRes goodsRes = goodsMapper.getGoodsByIdAndSupplierId(goodId, supplierId);
		if(goodsRes!=null){
			goodsMapper.revieweGoodById(goodId);
		}
	}
	@Override
	public void cancelRevieweGood(Integer supplierId, Integer goodId) {
		GoodsRes goodsRes = goodsMapper.getGoodsByIdAndSupplierId(goodId, supplierId);
		if(goodsRes!=null){
			goodsMapper.cancelRevieweGoodById(goodId);
		}
	}

	@Override
	@Transactional
	public void shelvesGood(Integer supplierId, Integer goodId) {
		GoodsRes goodsRes = goodsMapper.getGoodsByIdAndSupplierId(goodId, supplierId);
		if(goodsRes!=null){
			goodsMapper.shelvesGoodById(goodId);
		}
	}
	
	@Override
	@Transactional
	public void shelvesGoodAdmin(Integer goodId) {
		GoodsRes goodsRes = goodsMapper.getGoodsDetailById(goodId);
		if(goodsRes!=null){
			goodsMapper.shelvesGoodById(goodId);
		}
	}

	@Override
	@Transactional
	public void xiajiaGoodAdmin(Integer goodId) {
		GoodsRes goodsRes = goodsMapper.getGoodsDetailById(goodId);
		if(goodsRes!=null){
			goodsMapper.xiajiaGoodById(goodId);
		}
	}
	@Override
	@Transactional
	public void reviewSuccGoodAdmin(Integer goodId) {
		GoodsRes goodsRes = goodsMapper.getGoodsDetailById(goodId);
		if(goodsRes!=null){
			goodsMapper.reviewSuccessGoodById(goodId);
		}
	}
	
	@Override
	public void reviewFailGoodAdmin(Integer goodId) {
		GoodsRes goodsRes = goodsMapper.getGoodsDetailById(goodId);
		if(goodsRes!=null){
			goodsMapper.reviewFailGoodById(goodId);
		}
	}

	@Override
	@Transactional
	public void xiajiaGood(Integer supplierId, Integer goodId) {
		GoodsRes goodsRes = goodsMapper.getGoodsByIdAndSupplierId(goodId, supplierId);
		if(goodsRes!=null){
			goodsMapper.xiajiaGoodById(goodId);
		}
	}

	@Override
	@Transactional
	public Integer addGoodCollect(GoodCollect goodCollect) {
		GoodCollect db = goodCollectMapper.findGoodCollectByUserIdAndGoodId(goodCollect.getUserId(), goodCollect.getGoodId());
		if(db != null){
			throw new ShopServerException(ErrorConstant.OPERATION_ERROR.getErrorCode(), "您已经收藏该商品了"); 
		}
		return goodCollectMapper.addGoodCollect(goodCollect);
	}

	

	@Override
	@Transactional
	public void deleteGoodCollect(Integer id) {
		GoodCollect gc = goodCollectMapper.findGoodCollectById(id);
		if(gc!=null){
			goodCollectMapper.deleteGoodCollect(id);
		}
	}

	@Override
	public UserArea findUserAreaByUserId(Integer userId) {
		return userAreaMapper.findUserAreaByUserId(userId);
	}

	@Override
	public List<GoodsRes> findGoods(GoodsReq goodsReq) {
		return goodsMapper.findGoods(goodsReq);
	}

	@Override
	public List<GoodsRes> managerListPage(GoodsReq goodsReq) {
//		Page<GoodsRes> page = new Page<GoodsRes>(goodsReq.getCurrentPage());
//		page.setPageSize(goodsReq.getPageSize());
//		if(goodsReq.getCurrentPage() != null && goodsReq.getCurrentPage() > 0){
//			goodsReq.setStart((goodsReq.getCurrentPage()-1)*goodsReq.getPageSize());
//		}else {
//			goodsReq.setStart(0);
//		}
//		goodsReq.setLimit(goodsReq.getPageSize());
//		page.setTotalRow(this.countManagerList(goodsReq));
		List<GoodsRes> list = this.managerList(goodsReq);
		for(GoodsRes gr : list){
			if(StringUtils.isNotBlank(gr.getThumbnailImage())){
				gr.setThumbnailImage(FileStoreUtil.getBaseUrl().concat(gr.getThumbnailImage()));
			}
			logger.info("gr.getId() : {}",gr.getId()); 
			List<GoodsSpecification> goodsSpecificationList = goodsSpecificationMapper.findGoodsSpecificationsByGoodIdAll(gr.getId());
			logger.info("goodsSpecificationList : {}",goodsSpecificationList);
			gr.setSpecifications(goodsSpecificationList);
			
			List<MarketingActivityGoods> marketingActivityList = goodsMapper.findGoodMarketingActivityByGoodId(gr.getId());
			gr.setMarketingActivityList(marketingActivityList);
		}
//		page.setResultRows(list);
		return list;
	}

	@Override
	public List<GoodHotSearch> allGoodHotSearchs() {
		return goodHotSearchMapper.listGoodHotSearchs();
	}

	@Override
	public List<Category> categoryList4Good() {
		return goodsMapper.categoryList4Good();
	}
	
	@Override
	public List<Category> categoryList4ShopGood() {
		return goodsMapper.categoryList4ShopGood();
	}

	@Override
	public Integer saveCategory(Category category) {
		if(category.getParentId() == null || category.getParentId() <  0){
			category.setParentId(0);
			category.setLevel(1);
		}else {
			category.setLevel(2);
		}
		category.setShopUse(0);
		return goodsMapper.saveCategory(category);
	}

	@Override
	public Integer addFirstHomeComment(Integer categoryId) {
		return goodsMapper.addFirstHomeComment(categoryId);
	}

	@Override
	public Integer cancelFirstHomeComment(Integer categoryId) {
		return goodsMapper.cancelFirstHomeComment(categoryId);
	}

	@Override
	public Integer addHotSell(Integer categoryId) {
		return goodsMapper.addHotSell(categoryId);
	}

	@Override
	public Integer cancelHotSell(Integer categoryId) {
		return goodsMapper.cancelHotSell(categoryId);
	}

	@Override
	public Integer updateCategoryShopUse(Integer id) {
		return goodsMapper.updateCategoryShopUse(id);
	}

	@Override
	public Integer cancelCategoryShopUse(Integer id) {
		return goodsMapper.cancelCategoryShopUse(id);
	}

	@Override
	public Integer addGoodsSpecification(GoodsSpecification goodsSpecification) {
		List<GoodsSpecification> list = new ArrayList<GoodsSpecification>();
		list.add(goodsSpecification);
		goodsSpecificationMapper.batchSaveGoodsSpecifications(list);
		return 0;
	}

	@Override
	public Integer updateGoodsSpecification(GoodsSpecification goodsSpecification) {
		return goodsSpecificationMapper.update(goodsSpecification);
	}

	@Override
	public Integer addGoodImage(List<GoodsImage> list) {
		if(CollectionUtils.isNotEmpty(list)){
			goodsMapper.batchSaveGoodsImages(list);
		}
		return 0;
	}

	@Override
	public Integer addHomeBanner(Integer categoryId, String path) {
		
		bannerCategoryMapper.insertBannerCategory(categoryId);
		goodsMapper.updateCategoryImage(categoryId, path);
		return 1;
	}

	@Override
	public Integer cancelHomeBanner(Integer categoryId) {
		goodsMapper.updateCategoryImage(categoryId, null);
		return bannerCategoryMapper.deleteBannerCategory(categoryId);
	}

	@Override
	public void addMarketingActivity(Integer goodsId, Integer marketingActivityId) {
		goodsMapper.addMarketingActivity(goodsId, marketingActivityId);
	}

	@Override
	public void deleteMarketingActivity(Integer goodsId, Integer marketingActivityId) {
		goodsMapper.deleteMarketingActivity(goodsId, marketingActivityId);
	}
	
	
}